import { store } from '..';
import { RouterHistory } from '@/utils/interface';

export const useUtils = defineStore('utils', {
  state: () => ({
    isDark: false,
    routers: [],
    routerHistory: [
      { content: '成绩分析', id: 8, path: '/home/results', closable: false },
    ] as RouterHistory[],
    activeKey: 8,
  }),
  actions: {
    setDark(dark: boolean) {
      this.isDark = dark;
    },
    setRouters(routers) {
      this.routers = routers;
    },
    // 添加路由历史记录
    addRouterHistory(router: RouterHistory) {
      if (
        router.id === 8 ||
        router.content === '403' ||
        router.content === '404' ||
        router.content === undefined ||
        router.content === '登录'
      )
        return;
      !JSON.stringify(this.routerHistory).includes(JSON.stringify(router))
        ? this.routerHistory.push(router)
        : (this.activeKey = router.id);
    },
    setActiveKey(id: number) {
      this.activeKey = id;
    },
    getRoleName(id: number): string {
      if (id === 1) {
        return '超级管理员';
      } else if (id === 2) {
        return '学生管理员';
      } else if (id === 3) {
        return '题库管理员';
      } else if (id === 4) {
        return '考试管理员';
      }
      return '';
    },
    clearHistoryRouter() {
      this.routerHistory = [];
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        storage: localStorage,
        paths: ['routers'],
      },
    ],
  },
});

export function useUtilsStoreWithOut() {
  return useUtils(store);
}
